// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyAxesArray.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>
#include <PyAxisAttributes.h>

// ****************************************************************************
// Module: PyAxesArray
//
// Purpose:
//   Contains the properties for the array axes.
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a AxesArray.
//
struct AxesArrayObject
{
    PyObject_HEAD
    AxesArray *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewAxesArray(int);
std::string
PyAxesArray_ToString(const AxesArray *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    if(atts->GetVisible())
        snprintf(tmpStr, 1000, "%svisible = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%svisible = 0\n", prefix);
    str += tmpStr;
    if(atts->GetTicksVisible())
        snprintf(tmpStr, 1000, "%sticksVisible = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sticksVisible = 0\n", prefix);
    str += tmpStr;
    if(atts->GetAutoSetTicks())
        snprintf(tmpStr, 1000, "%sautoSetTicks = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sautoSetTicks = 0\n", prefix);
    str += tmpStr;
    if(atts->GetAutoSetScaling())
        snprintf(tmpStr, 1000, "%sautoSetScaling = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sautoSetScaling = 0\n", prefix);
    str += tmpStr;
    snprintf(tmpStr, 1000, "%slineWidth = %d\n", prefix, atts->GetLineWidth());
    str += tmpStr;
    { // new scope
        std::string objPrefix(prefix);
        objPrefix += "axes.";
        str += PyAxisAttributes_ToString(&atts->GetAxes(), objPrefix.c_str(), forLogging);
    }
    return str;
}

static PyObject *
AxesArray_Notify(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxesArray_SetVisible(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the visible in the object.
    obj->data->SetVisible(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxesArray_GetVisible(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetVisible()?1L:0L);
    return retval;
}

/*static*/ PyObject *
AxesArray_SetTicksVisible(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the ticksVisible in the object.
    obj->data->SetTicksVisible(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxesArray_GetTicksVisible(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetTicksVisible()?1L:0L);
    return retval;
}

/*static*/ PyObject *
AxesArray_SetAutoSetTicks(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the autoSetTicks in the object.
    obj->data->SetAutoSetTicks(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxesArray_GetAutoSetTicks(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetAutoSetTicks()?1L:0L);
    return retval;
}

/*static*/ PyObject *
AxesArray_SetAutoSetScaling(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the autoSetScaling in the object.
    obj->data->SetAutoSetScaling(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxesArray_GetAutoSetScaling(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetAutoSetScaling()?1L:0L);
    return retval;
}

/*static*/ PyObject *
AxesArray_SetLineWidth(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ int");
    }

    Py_XDECREF(packaged_args);

    // Set the lineWidth in the object.
    obj->data->SetLineWidth(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxesArray_GetLineWidth(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetLineWidth()));
    return retval;
}

/*static*/ PyObject *
AxesArray_SetAxes(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;

    PyObject *newValue = NULL;
    if(!PyArg_ParseTuple(args, "O", &newValue))
        return NULL;
    if(!PyAxisAttributes_Check(newValue))
        return PyErr_Format(PyExc_TypeError, "Field axes can be set only with AxisAttributes objects");

    obj->data->SetAxes(*PyAxisAttributes_FromPyObject(newValue));

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxesArray_GetAxes(PyObject *self, PyObject *args)
{
    AxesArrayObject *obj = (AxesArrayObject *)self;
    // Since the new object will point to data owned by this object,
    // we need to increment the reference count.
    Py_INCREF(self);

    PyObject *retval = PyAxisAttributes_Wrap(&obj->data->GetAxes());
    // Set the object's parent so the reference to the parent can be decref'd
    // when the child goes out of scope.
    PyAxisAttributes_SetParent(retval, self);

    return retval;
}



PyMethodDef PyAxesArray_methods[AXESARRAY_NMETH] = {
    {"Notify", AxesArray_Notify, METH_VARARGS},
    {"SetVisible", AxesArray_SetVisible, METH_VARARGS},
    {"GetVisible", AxesArray_GetVisible, METH_VARARGS},
    {"SetTicksVisible", AxesArray_SetTicksVisible, METH_VARARGS},
    {"GetTicksVisible", AxesArray_GetTicksVisible, METH_VARARGS},
    {"SetAutoSetTicks", AxesArray_SetAutoSetTicks, METH_VARARGS},
    {"GetAutoSetTicks", AxesArray_GetAutoSetTicks, METH_VARARGS},
    {"SetAutoSetScaling", AxesArray_SetAutoSetScaling, METH_VARARGS},
    {"GetAutoSetScaling", AxesArray_GetAutoSetScaling, METH_VARARGS},
    {"SetLineWidth", AxesArray_SetLineWidth, METH_VARARGS},
    {"GetLineWidth", AxesArray_GetLineWidth, METH_VARARGS},
    {"SetAxes", AxesArray_SetAxes, METH_VARARGS},
    {"GetAxes", AxesArray_GetAxes, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
AxesArray_dealloc(PyObject *v)
{
   AxesArrayObject *obj = (AxesArrayObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *AxesArray_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyAxesArray_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "visible") == 0)
        return AxesArray_GetVisible(self, NULL);
    if(strcmp(name, "ticksVisible") == 0)
        return AxesArray_GetTicksVisible(self, NULL);
    if(strcmp(name, "autoSetTicks") == 0)
        return AxesArray_GetAutoSetTicks(self, NULL);
    if(strcmp(name, "autoSetScaling") == 0)
        return AxesArray_GetAutoSetScaling(self, NULL);
    if(strcmp(name, "lineWidth") == 0)
        return AxesArray_GetLineWidth(self, NULL);
    if(strcmp(name, "axes") == 0)
        return AxesArray_GetAxes(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyAxesArray_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyAxesArray_methods[i].ml_name),
                PyString_FromString(PyAxesArray_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyAxesArray_methods, self, name);
}

int
PyAxesArray_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "visible") == 0)
        obj = AxesArray_SetVisible(self, args);
    else if(strcmp(name, "ticksVisible") == 0)
        obj = AxesArray_SetTicksVisible(self, args);
    else if(strcmp(name, "autoSetTicks") == 0)
        obj = AxesArray_SetAutoSetTicks(self, args);
    else if(strcmp(name, "autoSetScaling") == 0)
        obj = AxesArray_SetAutoSetScaling(self, args);
    else if(strcmp(name, "lineWidth") == 0)
        obj = AxesArray_SetLineWidth(self, args);
    else if(strcmp(name, "axes") == 0)
        obj = AxesArray_SetAxes(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
AxesArray_print(PyObject *v, FILE *fp, int flags)
{
    AxesArrayObject *obj = (AxesArrayObject *)v;
    fprintf(fp, "%s", PyAxesArray_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
AxesArray_str(PyObject *v)
{
    AxesArrayObject *obj = (AxesArrayObject *)v;
    return PyString_FromString(PyAxesArray_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *AxesArray_Purpose = "Contains the properties for the array axes.";
#else
static char *AxesArray_Purpose = "Contains the properties for the array axes.";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(AxesArrayType,         \
                  "AxesArray",           \
                  AxesArrayObject,       \
                  AxesArray_dealloc,     \
                  AxesArray_print,       \
                  PyAxesArray_getattr,   \
                  PyAxesArray_setattr,   \
                  AxesArray_str,         \
                  AxesArray_Purpose,     \
                  AxesArray_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
AxesArray_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &AxesArrayType
         || Py_TYPE(other) != &AxesArrayType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    AxesArray *a = ((AxesArrayObject *)self)->data;
    AxesArray *b = ((AxesArrayObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static AxesArray *defaultAtts = 0;
static AxesArray *currentAtts = 0;

static PyObject *
NewAxesArray(int useCurrent)
{
    AxesArrayObject *newObject;
    newObject = PyObject_NEW(AxesArrayObject, &AxesArrayType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new AxesArray(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new AxesArray(*defaultAtts);
    else
        newObject->data = new AxesArray;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapAxesArray(const AxesArray *attr)
{
    AxesArrayObject *newObject;
    newObject = PyObject_NEW(AxesArrayObject, &AxesArrayType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (AxesArray *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
AxesArray_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewAxesArray(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef AxesArrayMethods[] = {
    {"AxesArray", AxesArray_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *AxesArrayObserver = 0;

std::string
PyAxesArray_GetLogString()
{
    std::string s("AxesArray = AxesArray()\n");
    if(currentAtts != 0)
        s += PyAxesArray_ToString(currentAtts, "AxesArray.", true);
    return s;
}

static void
PyAxesArray_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("AxesArray = AxesArray()\n");
        s += PyAxesArray_ToString(currentAtts, "AxesArray.", true);
        cb(s);
    }
}

void
PyAxesArray_StartUp(AxesArray *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyAxesArray_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(AxesArrayObserver == 0)
    {
        AxesArrayObserver = new ObserverToCallback(subj,
            PyAxesArray_CallLogRoutine, (void *)data);
    }

}

void
PyAxesArray_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete AxesArrayObserver;
    AxesArrayObserver = 0;
}

PyMethodDef *
PyAxesArray_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return AxesArrayMethods;
}

bool
PyAxesArray_Check(PyObject *obj)
{
    return (obj->ob_type == &AxesArrayType);
}

AxesArray *
PyAxesArray_FromPyObject(PyObject *obj)
{
    AxesArrayObject *obj2 = (AxesArrayObject *)obj;
    return obj2->data;
}

PyObject *
PyAxesArray_New()
{
    return NewAxesArray(0);
}

PyObject *
PyAxesArray_Wrap(const AxesArray *attr)
{
    return WrapAxesArray(attr);
}

void
PyAxesArray_SetParent(PyObject *obj, PyObject *parent)
{
    AxesArrayObject *obj2 = (AxesArrayObject *)obj;
    obj2->parent = parent;
}

void
PyAxesArray_SetDefaults(const AxesArray *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new AxesArray(*atts);
}

